/**
 * Copyright (C) 2011-2012 Luca Li Greci
 *
 * This file is part of pojo-creator
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pojocreator.provider.collection;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.math.RandomUtils;

/**
 * This class implements the required method {@link com.pojocreator.provider.DataProvider#data()} to
 * provide a random value from a given collection of elements.
 * 
 * @author Luca Li Greci
 * 
 * @param <T> Data type to provide
 */
public class RandomFromCollection < T > extends AbstractFromCollection < T > {
    /**
     * Creates a DataProvider using the element in the collection.
     * 
     * @param values array of elements type T.
     */
    public RandomFromCollection(T... values) {
        super(values);
    }

    /**
     * This method returns an element from the array of elements passed in.
     * <p>
     * <b>N.B.</b> This method can return null if the array of element is null.
     * or empty.
     * 
     * @return an element picked up randomly from the array.
     */
    @Override
    public T data() {
        if (CollectionUtils.isEmpty(values)) {
            return null;
        }
        return values.get(RandomUtils.nextInt(values.size()));
    }

}
